<?php
namespace app\api\controller;
use think\facade\Cache;
use app\parent\api;

class Common extends Api{
	public function get_X_Token($x_appid , $x_secret){
        if(Cache::get('xcx_token')){
            $token = Cache::get('xcx_token');
        }else{
            $url   = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $x_appid . '&secret=' . $x_secret;
            $res   = $this->http_get($url);
            $res   = json_decode($res);
            Cache::set('xcx_token' , $res->access_token , $res->expires_in - 1);
            $token = $res->access_token;
        }
        return $token;

    }

    protected function http_get($url){
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        $res = curl_exec($ch);
        curl_close($ch);
        return $res;
    }

    protected function ihttp_post($url, $post_data){
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        $res = curl_exec($ch);
        curl_close($ch);
        return $res;
    }
}